#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student
{
    char name[50];
    int age;
}Student;

typedef struct Node
{
    Student data;
    struct Node *next;
}Node;

//创建新节点
Node* createNode(Student s)
{
    Node * newNode = (Node*)malloc(sizeof(Node));

    if(newNode == NULL)
    {
        printf("内存分配失败！\n");
        return NULL;
    }

    strcpy(newNode->data.name,s.name);
    newNode->data.age = s.age;
    newNode->next = NULL;

    return newNode;
}

//插入新节点
Node* insertAtHead(Node* head, Student s)
{
    Node * newNode = createNode(s);

    if(newNode == NULL)
        return head;
    
    newNode->next = head;

    return newNode;
}

void printList(Node* head)
{
    Node * temp = head;

    if(temp == NULL)
    {
        printf("链表为空！\n");
        return;
    }

    while(temp != NULL)
    {
        printf("name:%s,age:%d\n",temp->data.name,temp->data.age);
        temp = temp->next;
    }
}

void freeList(Node* head)
{
    Node * temp = head;
    Node * p;

    while(temp != NULL)
    {
        p = temp->next;
        free(temp);
        temp = p;
    }
    printf("已释放！\n");
}

int main(int argc, char const *argv[])
{
    Node* head = NULL;
    Student s1 = {"zhang",20};
    Student s2 = {"kou",21};

    head = insertAtHead(head,s1);
    head = insertAtHead(head,s2);

    printList(head);

    freeList(head);

    return 0;
}
